using S.O.Domain.Medical;
using S.O.Interstructrue;
using Yitter.IdGenerator;

namespace S.O.Api.Read.DataSeed
{
    /// <summary>
    /// 医疗数据种子类
    /// 用于初始化患者和挂号记录的测试数据
    /// </summary>
    public static class MedicalDataSeed
    {
        /// <summary>
        /// 初始化医疗测试数据
        /// </summary>
        /// <param name="context">数据库上下文</param>
        public static void SeedMedicalData(RBACDbContext context)
        {
            // 检查是否已存在患者数据
            if (!context.Patients.Any())
            {
                // 患者数据不存在，创建患者和挂号数据
                SeedPatientsAndRegistrations(context);
            }

            // 检查是否已存在处方数据
            if (!context.Prescriptions.Any())
            {
                // 处方数据不存在，创建处方和药品数据
                SeedPrescriptionsAndMedicines(context);
            }
            else
            {
                // 检查是否存在有效的处方药品数据（quantity > 0）
                var hasValidMedicines = context.PrescriptionMedicines
                    .Any(pm => pm.Quantity > 0 && pm.UnitPrice > 0);
                
                if (!hasValidMedicines)
                {
                    // 清空现有的无效处方数据，重新创建
                    var existingMedicines = context.PrescriptionMedicines.ToList();
                    var existingPrescriptions = context.Prescriptions.ToList();
                    
                    context.PrescriptionMedicines.RemoveRange(existingMedicines);
                    context.Prescriptions.RemoveRange(existingPrescriptions);
                    context.SaveChanges();
                    
                    // 重新创建处方和药品数据
                    SeedPrescriptionsAndMedicines(context);
                }
            }
        }

        /// <summary>
        /// 创建患者和挂号数据
        /// </summary>
        /// <param name="context">数据库上下文</param>
        private static void SeedPatientsAndRegistrations(RBACDbContext context)
        {

            // 创建患者数据
            var patients = new List<PatientModel>
            {
                new PatientModel
                {
                    Id = YitIdHelper.NextId(),
                    PatientName = "张三",
                    CardNo = "20001",
                    Gender = 1, // 男
                    Age = 35,
                    Phone = "13800138001",
                    IdCard = "110101198801011234",
                    Address = "北京市朝阳区",
                    IsEnable = true,
                    CreateTime = DateTime.Now,
                    CreateUser = "系统",
                    IsDeleted = false
                },
                new PatientModel
                {
                    Id = YitIdHelper.NextId(),
                    PatientName = "李四",
                    CardNo = "20002",
                    Gender = 0, // 女
                    Age = 28,
                    Phone = "13800138002",
                    IdCard = "110101199201011234",
                    Address = "北京市海淀区",
                    IsEnable = true,
                    CreateTime = DateTime.Now,
                    CreateUser = "系统",
                    IsDeleted = false
                },
                new PatientModel
                {
                    Id = YitIdHelper.NextId(),
                    PatientName = "王五",
                    CardNo = "20003",
                    Gender = 1, // 男
                    Age = 42,
                    Phone = "13800138003",
                    IdCard = "110101198001011234",
                    Address = "北京市西城区",
                    IsEnable = true,
                    CreateTime = DateTime.Now,
                    CreateUser = "系统",
                    IsDeleted = false
                },
                new PatientModel
                {
                    Id = YitIdHelper.NextId(),
                    PatientName = "赵六",
                    CardNo = "20004",
                    Gender = 0, // 女
                    Age = 31,
                    Phone = "13800138004",
                    IdCard = "110101199001011234",
                    Address = "北京市东城区",
                    IsEnable = true,
                    CreateTime = DateTime.Now,
                    CreateUser = "系统",
                    IsDeleted = false
                }
            };

            context.Patients.AddRange(patients);
            context.SaveChanges();

            // 创建挂号记录数据
            var registrations = new List<RegistrationModel>
            {
                new RegistrationModel
                {
                    Id = YitIdHelper.NextId(),
                    RegistrationNo = "GH202412010001",
                    PatientId = patients[0].Id,
                    CardNo = "20001",
                    PatientName = "张三",
                    RegistrationType = "普通号",
                    RegistrationFee = 10.00m,
                    OtherFee = 5.00m,
                    Department = "内科",
                    Doctor = "李医生",
                    RegistrationDate = DateTime.Now.AddDays(-1),
                    VisitDate = DateTime.Now,
                    Status = "已挂号",
                    IsEnable = true,
                    CreateTime = DateTime.Now.AddDays(-1),
                    CreateUser = "系统",
                    IsDeleted = false
                },
                new RegistrationModel
                {
                    Id = YitIdHelper.NextId(),
                    RegistrationNo = "GH202412010002",
                    PatientId = patients[1].Id,
                    CardNo = "20002",
                    PatientName = "李四",
                    RegistrationType = "专家号",
                    RegistrationFee = 50.00m,
                    OtherFee = 10.00m,
                    Department = "妇科",
                    Doctor = "王医生",
                    RegistrationDate = DateTime.Now,
                    VisitDate = DateTime.Now.AddDays(1),
                    Status = "已挂号",
                    IsEnable = true,
                    CreateTime = DateTime.Now,
                    CreateUser = "系统",
                    IsDeleted = false
                },
                new RegistrationModel
                {
                    Id = YitIdHelper.NextId(),
                    RegistrationNo = "GH202412010003",
                    PatientId = patients[2].Id,
                    CardNo = "20003",
                    PatientName = "王五",
                    RegistrationType = "普通号",
                    RegistrationFee = 10.00m,
                    OtherFee = 5.00m,
                    Department = "外科",
                    Doctor = "张医生",
                    RegistrationDate = DateTime.Now.AddDays(-2),
                    VisitDate = DateTime.Now.AddDays(-1),
                    Status = "已就诊",
                    IsEnable = true,
                    CreateTime = DateTime.Now.AddDays(-2),
                    CreateUser = "系统",
                    IsDeleted = false
                },
                new RegistrationModel
                {
                    Id = YitIdHelper.NextId(),
                    RegistrationNo = "GH202412010004",
                    PatientId = patients[3].Id,
                    CardNo = "20004",
                    PatientName = "赵六",
                    RegistrationType = "急诊号",
                    RegistrationFee = 20.00m,
                    OtherFee = 15.00m,
                    Department = "急诊科",
                    Doctor = "陈医生",
                    RegistrationDate = DateTime.Now,
                    VisitDate = DateTime.Now,
                    Status = "就诊中",
                    IsEnable = true,
                    CreateTime = DateTime.Now,
                    CreateUser = "系统",
                    IsDeleted = false
                },
                new RegistrationModel
                {
                    Id = YitIdHelper.NextId(),
                    RegistrationNo = "GH202412010005",
                    PatientId = patients[0].Id,
                    CardNo = "20001",
                    PatientName = "张三",
                    RegistrationType = "专家号",
                    RegistrationFee = 50.00m,
                    OtherFee = 10.00m,
                    Department = "心内科",
                    Doctor = "刘医生",
                    RegistrationDate = DateTime.Now,
                    VisitDate = DateTime.Now.AddDays(2),
                    Status = "已挂号",
                    IsEnable = true,
                    CreateTime = DateTime.Now,
                    CreateUser = "系统",
                    IsDeleted = false
                }
            };

            context.Registrations.AddRange(registrations);
            context.SaveChanges();
        }

        /// <summary>
        /// 创建处方和药品数据
        /// </summary>
        /// <param name="context">数据库上下文</param>
        private static void SeedPrescriptionsAndMedicines(RBACDbContext context)
        {
            // 获取现有患者数据
            var patients = context.Patients.Where(p => !p.IsDeleted).ToList();
            if (!patients.Any())
            {
                return; // 没有患者数据，无法创建处方
            }

            // 创建处方数据
            var prescriptions = new List<PrescriptionModel>
            {
                new PrescriptionModel
                {
                    Id = "PR" + DateTime.Now.ToString("yyyyMMdd") + "000001",
                    PatientId = patients[0].Id,
                    PatientName = "张三",
                    PrescriptionType = "西药",
                    Diagnosis = "感冒",
                    Remark = "注意休息，多喝水",
                    Doctor = "李医生",
                    Status = "已开具",
                    CreateTime = DateTime.Now.AddDays(-1),
                    UpdateTime = DateTime.Now.AddDays(-1),
                    IsEnable = true,
                    CreateUser = "李医生",
                    IsDeleted = false
                },
                new PrescriptionModel
                {
                    Id = "PR" + DateTime.Now.ToString("yyyyMMdd") + "000002",
                    PatientId = patients[3].Id,
                    PatientName = "赵六",
                    PrescriptionType = "西药",
                    Diagnosis = "急性胃炎",
                    Remark = "饭后服用",
                    Doctor = "陈医生",
                    Status = "已开具",
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    IsEnable = true,
                    CreateUser = "陈医生",
                    IsDeleted = false
                }
            };

            context.Prescriptions.AddRange(prescriptions);
            context.SaveChanges();

            // 创建处方药品明细数据
            var prescriptionMedicines = new List<PrescriptionMedicineModel>
            {
                // 张三的处方药品
                new PrescriptionMedicineModel
                {
                    Id = YitIdHelper.NextId(),
                    PrescriptionId = prescriptions[0].Id,
                    MedicineName = "阿莫西林胶囊",
                    Specification = "0.25g*24粒",
                    Dosage = "0.5g",
                    Frequency = "每日3次",
                    Duration = "7天",
                    Usage = "口服",
                    Remark = "饭后服用",
                    Quantity = 2,
                    UnitPrice = 15.50m,
                    Amount = 31.00m,
                    IsPaid = false,
                    SortOrder = 1,
                    CreateTime = DateTime.Now.AddDays(-1),
                    UpdateTime = DateTime.Now.AddDays(-1),
                    IsEnable = true,
                    CreateUser = "李医生",
                    IsDeleted = false
                },
                new PrescriptionMedicineModel
                {
                    Id = YitIdHelper.NextId(),
                    PrescriptionId = prescriptions[0].Id,
                    MedicineName = "感冒灵颗粒",
                    Specification = "10g*9袋",
                    Dosage = "10g",
                    Frequency = "每日3次",
                    Duration = "5天",
                    Usage = "口服",
                    Remark = "温水冲服",
                    Quantity = 1,
                    UnitPrice = 12.80m,
                    Amount = 12.80m,
                    IsPaid = false,
                    SortOrder = 2,
                    CreateTime = DateTime.Now.AddDays(-1),
                    UpdateTime = DateTime.Now.AddDays(-1),
                    IsEnable = true,
                    CreateUser = "李医生",
                    IsDeleted = false
                },
                // 赵六的处方药品
                new PrescriptionMedicineModel
                {
                    Id = YitIdHelper.NextId(),
                    PrescriptionId = prescriptions[1].Id,
                    MedicineName = "奥美拉唑肠溶胶囊",
                    Specification = "20mg*14粒",
                    Dosage = "20mg",
                    Frequency = "每日1次",
                    Duration = "14天",
                    Usage = "口服",
                    Remark = "空腹服用",
                    Quantity = 1,
                    UnitPrice = 28.60m,
                    Amount = 28.60m,
                    IsPaid = false,
                    SortOrder = 1,
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    IsEnable = true,
                    CreateUser = "陈医生",
                    IsDeleted = false
                },
                new PrescriptionMedicineModel
                {
                    Id = YitIdHelper.NextId(),
                    PrescriptionId = prescriptions[1].Id,
                    MedicineName = "多潘立酮片",
                    Specification = "10mg*30片",
                    Dosage = "10mg",
                    Frequency = "每日3次",
                    Duration = "10天",
                    Usage = "口服",
                    Remark = "饭前服用",
                    Quantity = 1,
                    UnitPrice = 18.90m,
                    Amount = 18.90m,
                    IsPaid = false,
                    SortOrder = 2,
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    IsEnable = true,
                    CreateUser = "陈医生",
                    IsDeleted = false
                }
            };

            context.PrescriptionMedicines.AddRange(prescriptionMedicines);
            context.SaveChanges();
        }
    }
}